package m_dsp_adv_creative

import (
	"gopkg.in/mgo.v2"
	"gopkg.in/mgo.v2/bson"
	"time"
	"wx-api/dsp-api/common"
	"wx-api/dsp-api/config"
)

type DspAdvCreative struct {
	Id         bson.ObjectId       `bson:"_id" json:"id,omitempty"`
	Code       string              `bson:"code" json:"code,omitempty"`               //id
	AdvId      string              `bson:"adv_id" json:"adv_id,omitempty"`           //所属广告主id
	PlanId     string              `bson:"plan_id" json:"plan_id,omitempty"`         //所属计划id
	PlanName   string              `bson:"plan_name" json:"plan_name,omitempty"`     //所属计划id
	AppId      string              `bson:"app_id" json:"app_id,omitempty"`           //关联appid
	AppName    string              `bson:"app_name" json:"app_name,omitempty"`       //关联appid
	Name       string              `bson:"name" json:"name,omitempty"`               //名称
	VideoAdm   []DspAdvCreativeAdm `bson:"video_adm" json:"video_adm,omitempty"`     //基础素材
	ImgAdm     []DspAdvCreativeAdm `bson:"img_adm" json:"img_adm,omitempty"`         //基础素材
	Title      []string            `bson:"title" json:"title,omitempty"`             //素材文案
	Link       string              `bson:"link" json:"link,omitempty"`               //落地页
	DpLink     string              `bson:"dplink" json:"dplink,omitempty"`           //安卓直达链接
	UnLink     string              `bson:"unlink" json:"unlink,omitempty"`           //ios直达链接
	Icon       string              `bson:"icon" json:"icon,omitempty"`               //产品icon
	Budget     float64             `bson:"budget" json:"budget,omitempty"`           //每日预算
	Price      float64             `bson:"price" json:"price,omitempty"`             //广告出价
	Flag       int                 `bson:"flag" json:"flag,omitempty"`               //1 开启 2 关闭
	Status     int                 `bson:"status" json:"status,omitempty"`           //1 正常 2 删除
	CreateTime int64               `bson:"create_time" json:"create_time,omitempty"` //创建时间
	UpdateTime int64               `bson:"update_time" json:"update_time,omitempty"` //更新时间
	Version    string              `bson:"version" json:"version,omitempty"`         //版本号
}

type DspAdvCreativeAdm struct {
	AdmType  string `bson:"adm_type" json:"adm_type,omitempty"` //1图片，2视频
	AdName   string `bson:"ad_name" json:"ad_name,omitempty"`   //素材名称
	AdFile   string `bson:"ad_file" json:"ad_file,omitempty"`   //素材文件名
	AdUrls   string `bson:"ad_urls" json:"ad_urls,omitempty"`   //素材url
	Desc     string `bson:"desc" json:"desc,omitempty"`         //描述
	Width    int    `bson:"width" json:"width,omitempty"`       //宽
	Height   int    `bson:"height" json:"height,omitempty"`     //高
	Duration int    `bson:"duration" json:"duration,omitempty"` //时长，单位秒
	Size     int32  `bson:"size" json:"size,omitempty"`         //视频大小，KB
	CImg     string `bson:"cimg" json:"cimg,omitempty"`         //封面
}

func dbGet() *common.TkMongo {
	return config.MgTableDspAdvCreative
}

func Save(a DspAdvCreative) (string, error) {
	a.Id = bson.NewObjectId()
	tmptime := time.Now().Unix()
	a.CreateTime = tmptime
	a.UpdateTime = tmptime
	a.Flag = 1
	a.Status = 1
	a.Version = config.SysVersion
	err := dbGet().Insert(a)
	return a.Id.Hex(), err
}

// 更新
func Update(a DspAdvCreative) error {
	m := bson.M{
		"name":        a.Name,
		"video_adm":   a.VideoAdm,
		"img_adm":     a.ImgAdm,
		"app_id":      a.AppId,
		"title":       a.Title,
		"link":        a.Link,
		"dplink":      a.DpLink,
		"unlink":      a.UnLink,
		"budget":      a.Budget,
		"price":       a.Price,
		"update_time": time.Now().Unix(),
	}

	return dbGet().Set(bson.M{"_id": a.Id}, m)
}

func SelUpdate(id string, price float64, link, app_id, dplink, unlink string) error {
	m := bson.M{
		"update_time": time.Now().Unix(),
	}
	ised := false
	if price > 0 {
		m["price"] = price
		ised = true
	}
	if link != "" {
		m["link"] = link
		ised = true
	}
	if app_id != "" {
		m["app_id"] = app_id
		ised = true
	}
	if unlink != "" {
		m["unlink"] = unlink
		ised = true
	}
	if dplink != "" {
		m["dplink"] = dplink
		ised = true
	}
	if !ised {
		return nil
	}
	return dbGet().Set(bson.M{"_id": bson.ObjectIdHex(id)}, m)
}

// 更新状态
func UpdateFalg(id string, flag int) error {
	m := bson.M{
		"flag":        flag,
		"update_time": time.Now().Unix(),
	}

	return dbGet().Set(bson.M{"_id": bson.ObjectIdHex(id)}, m)
}

// 删除
func UpdateDel(id string, status int) error {
	m := bson.M{
		"status":      status,
		"update_time": time.Now().Unix(),
	}

	return dbGet().Set(bson.M{"_id": bson.ObjectIdHex(id)}, m)
}

// 获取所有
func GetAll() []DspAdvCreative {
	list := []DspAdvCreative{}
	_ = dbGet().All(nil, &list)
	return list
}

func GetWAll(a int64) []DspAdvCreative {
	list := []DspAdvCreative{}
	_ = dbGet().All(bson.M{"update_time": bson.M{"$gte": a}}, &list)
	return list
}

// 获取详情
func GetByCode(id string) (*DspAdvCreative, error) {
	a := &DspAdvCreative{}
	err := dbGet().FindOne(bson.M{"_id": bson.ObjectIdHex(id)}, a)
	if err != nil {
		if err == mgo.ErrNotFound {
			return nil, nil
		}
		return nil, err
	}
	return a, nil
}
func GetByCk(id string) (*DspAdvCreative, error) {
	a := &DspAdvCreative{}
	w := bson.M{"_id": bson.ObjectIdHex(id)}
	err := dbGet().FindOne(w, a)
	if err != nil {
		if err == mgo.ErrNotFound {
			return nil, nil
		}
		return nil, err
	}
	return a, nil
}

// 列表，带分页
func List(id, name, adx_uid string, flag, status, pageNum, pageSize int) (*common.PageInfo, error) {
	s := bson.M{}
	if id != "" {
		s["_id"] = bson.ObjectIdHex(id)
	}
	if name != "" {
		s["name"] = bson.M{"$regex": bson.RegEx{name, "i"}}
	}
	if adx_uid != "" {
		s["adx_uid"] = adx_uid
	}
	if flag > 0 {
		s["flag"] = flag
	}
	if status > 0 {
		s["status"] = status
	}
	var list []DspAdvCreative
	return dbGet().FindPageInfo(s, "", pageSize, pageNum, "-update_time", &list)
}
